"""
Author        yongfa
Date          2023-08-31 03:37:35
LastEditTime  2023-09-01 07:09:28
LastEditors   yongfa
Description   数据模型
"""

from app.extensions.sqlalchemy import db, EntityModel as Base
from app.extensions.utils import get_random_str, get_uuid
from app.extensions.utils.encryption import Prpcrypt


class Company(Base):
    __tablename__ = "company"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False, index=True, comment='名称')
    code = db.Column(db.String(50), comment='编码')
    level = db.Column(db.Integer, comment='等级')
    status = db.Column(db.Boolean, default=True, comment='状态')
    quota = db.Column(db.Integer, default=0, comment='额度')
    remark = db.Column(db.String(200), comment='备注')


class CompanyAccessKey(Base):
    __tablename__ = "company_access_key"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    company_id = db.Column(db.Integer, db.ForeignKey("company.id"), comment='公司ID')
    access_key = db.Column(db.String(32), nullable=False, index=True, default=get_random_str, comment='key')
    access_secret = db.Column(db.String(100), comment='secret')
    status = db.Column(db.Boolean, default=True, comment='状态')

    def __init__(self, *args, **kwargs) -> None:
        if self.access_secret is None:
            """加密"""
            aes = Prpcrypt()
            self.access_secret = aes.encrypt(get_uuid())
